package queryfish.db.db4o;

import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

public class Query {

	public static ArrayList<String> arrResult = new ArrayList<>();
	public static ArrayList<String> arrFishname = new ArrayList<>();
	public static ArrayList<String> imageName = new ArrayList<>();
	public static boolean flag = true;

	public static void checkQuery(ObjectContainer db, final String nameFish) {
		// hàm kiểm tra tên cá có tồn tại trong dữ liệu
		// không tồn tại sẽ trả về false
		// trả về số kết quả
		ObjectSet result = db.query(new Predicate<WaterFish>() {

			@Override
			public boolean match(WaterFish fish) {
				// TODO Auto-generated method stub
				return fish.getFish_name()
						.equals(nameFish.trim().toUpperCase())
						|| fish.getGeneral_information()
								.getVietnamese_name()
								.contains(
										"." + nameFish.trim().toLowerCase()
												+ ".");
			}

		});
		if (result.size() == 0) {
			flag = false;

		}
		listResult(result);

	}

	public static void queryFish(ObjectContainer db, String nameFish,
			String keyworld) {
		// toàn bộ hàm truy vấn
		checkQuery(db, nameFish.trim().toUpperCase());
		// kiểm tra tồn tại của dữ liệu
		// lấy kết quả từ hàm kiểm tra checkQuery
		if (flag == false) {
			arrResult.add("false");

		} else {
			// truy vấn theo tất cả các tên cá sau khi được kiểm tra
			for (int i = 0; i < arrFishname.size(); i++) {
				String str = arrFishname.get(i).toString();
				try {

					WaterFish fish = new WaterFish(str.toUpperCase());
					ObjectSet result = db.queryByExample(fish);
					while (result.hasNext()) {
						WaterFish wf = (WaterFish) result.next();
						String strResult = "";
						// all info about a fish
						if (keyworld.equals("")) {
							// to something
							imageName.add(wf.getImage().toString());
							strResult = wf.toString();
//							System.out.print(strResult+"\n");
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}						
							
						}
						// begin query general infor
						// all infor about general infor
						if (keyworld.equals("generalInfo")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information().toString();
							arrResult.add(keyworld);
							arrResult.add(wf.getBiology().getDistribution()
									.toString());
							StringTokenizer token = new StringTokenizer(
									strResult, "/");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about science name
						if (keyworld.equals("scienceName")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getScience_name().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about common name,will show vietnamese name
						if ((keyworld.equals("commonName"))
								|| (keyworld.equals("vnName"))) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getVietnamese_name().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about fish set
						if (keyworld.equals("fishSet")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getFish_set().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about fish class
						if (keyworld.equals("fishClass")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getFish_class().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about kind of fish
						if (keyworld.equals("kindOfFish")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getKind_of_fish().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about same name
						if (keyworld.equals("sameName")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getSame_name().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about english name
						if (keyworld.equals("engName")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getEnglish_name().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about original + distribution
						if (keyworld.equals("orgination")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getGeneral_information()
									.getOrigination().toString();
							arrResult.add(keyworld);
							arrResult.add(wf.getBiology().getDistribution()
									.toString());
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// end query general info
						// begin query biology
						// all infor about biology
						if (keyworld.equals("biology")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "/");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about distribution + original
						if (keyworld.equals("distribution")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getDistribution()
									.toString();
							arrResult.add(keyworld);
							arrResult.add(wf.getGeneral_information()
									.getOrigination().toString());
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about long of fish
						if (keyworld.equals("longFish")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getFish_long()
									.toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						if (keyworld.equals("waterTemparature")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getWater_temparature()
									.toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						if (keyworld.equals("dH")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getdH().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						if (keyworld.equals("pH")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getpH().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						if (keyworld.equals("repronduction")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getReproduction()
									.toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						if (keyworld.equals("waterField")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getBiology().getWater_field()
									.toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// end query bilogy
						// begin query culture tech
						// all infor about culture tech
						if (keyworld.equals("cultureTech")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology().toString();
							arrResult.add(keyworld);
							arrResult.add(wf.getBiology().getFeeding()
									.toString());
							StringTokenizer token = new StringTokenizer(
									strResult, "/");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about Aquarium
						if (keyworld.equals("designAquarium")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology()
									.getAquarium().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}

						}
						// about moss request
						if (keyworld.equals("mossRequest")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology()
									.getMoss_request().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}
						}
						// about light request
						if (keyworld.equals("lightRequest")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology()
									.getLight_request().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}
						}
						// about water filter request
						if (keyworld.equals("waterFilterRequest")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology()
									.getWater_filter_request().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}
						}
						// about aeration request
						if (keyworld.equals("aerationRequest")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology()
									.getAeration_request().toString();
							arrResult.add(keyworld);
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}
						}
						// about kind of food and feeding
						if (keyworld.equals("kindOfFood")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology()
									.getKind_of_food().toString();
							arrResult.add(keyworld);
							arrResult.add(wf.getBiology().getFeeding()
									.toString());
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}
						}
						// about caring and feed form
						if (keyworld.equals("caring")) {
							imageName.add(wf.getImage().toString());
							strResult = wf.getCulture_technology().getCaring()
									.toString();
							arrResult.add(keyworld);
							arrResult.add(wf.getCulture_technology()
									.getFeed_form());
							StringTokenizer token = new StringTokenizer(
									strResult, "\n");
							while (token.hasMoreTokens()) {
								arrResult.add(token.nextToken());
							}
						}
						// end query culture tech

					}
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Không Thể Truy Vấn Dữ Liệu !", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}

	}

	public static void listResult(ObjectSet<WaterFish> result) {
		try {
			while (result.hasNext()) {
				arrFishname.add(result.next().getFish_name().trim()
						.toUpperCase());
			}
		} catch (Exception e) {
		}
	}

}
